package com.maezia.tdr.common;

import com.maezia.tdr.common.annotation.NotNull;
import com.maezia.tdr.common.enums.StatusCodeEnum;
import com.maezia.tdr.common.exception.DriveException;
import lombok.SneakyThrows;
import org.springframework.util.ObjectUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


public class ModeValidator implements ConstraintValidator<NotNull, Object> {

    private StatusCodeEnum codeEnum;

    @Override
    public void initialize(NotNull annotation) {
        this.codeEnum = annotation.type();
    }

    @SneakyThrows
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if(ObjectUtils.isEmpty(value)){
            throw new DriveException(codeEnum);
        }

        if(value instanceof String && "".equals(value.toString().trim())){
            throw new DriveException(codeEnum);
        }
        return true;
    }
}
